package servlets;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Adresse;
import model.Teilnehmer;
import model.Termin;
import service.TerminplanerConnectionPool;
import service.TerminplanerService;

@WebServlet("/TerminServlet")
public class TerminServlet extends HttpServlet{
  private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

      Termin termin = null;
      Adresse adresse = null;
      ArrayList<Teilnehmer> teilnehmer = null;
      Long terminId = null;
      Connection con = null;
      try{
        con  = TerminplanerConnectionPool.getInstanz().getConnection();
        terminId = Long.valueOf(request.getParameter("TerminId"));
        termin = TerminplanerService.getInstanz().getTermin(con, terminId);
        adresse = TerminplanerService.getInstanz().getAdresseByTerminId(con, terminId);
        teilnehmer = TerminplanerService.getInstanz().getTeilnehmerByTerminId(con, terminId);
      }
      catch (Exception e)
      {request.setAttribute("exception", e.getMessage());
        request.getRequestDispatcher("/WEB-INF/jsps/error.jsp").forward(request, response);
        e.printStackTrace();
        return;
      }
      finally{
        TerminplanerConnectionPool.getInstanz().saveClose(con);
      }
      
      request.setAttribute("terminId", terminId);
      request.setAttribute("termin", termin);
      request.setAttribute("adresse", adresse);
      request.setAttribute("teilnehmer", teilnehmer);
      
      request.getRequestDispatcher("/WEB-INF/jsps/termin.jsp").forward(request, response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  

    }
    
  }


